package es.udc.emarketapp.http.view.actionForms;

import es.udc.emarketapp.http.view.applicationObjects.DateRanges;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.DefaultActionForm;

public class FindOrdersByDateForm extends DefaultActionForm {

    private String accountIdentifier;
    private Long accountIdentifierAsLong;
    private String startDay;
    private String startMonth;
    private String startYear;
    private String endDay;
    private String endMonth;
    private String endYear;
    private int startIndex;
    private int count;
    
    public FindOrdersByDateForm() {
        reset();
    }
    
    public String getAccountIdentifier() {
        return accountIdentifier;
    }
    
    public Long getAccountIdentifierAsLong() {
        return accountIdentifierAsLong;
    }

    public void setAccountIdentifier(String accountIdentifier) {
        this.accountIdentifier = accountIdentifier.trim();
    }
    
    public String getStartDay() {
        return startDay;
    }
    
    public void setStartDay(String startDay) {
        this.startDay = startDay;
    }
    
    public String getStartMonth() {
        return startMonth;
    }
    
    public void setStartMonth(String startMonth) {
        this.startMonth = startMonth;
    }
    
    public String getStartYear() {
        return startYear;
    }
    
    public void setStartYear(String startYear) {
        this.startYear = startYear;
    }
    
    public String getEndDay() {
        return endDay;
    }
    
    public void setEndDay(String endDay) {
        this.endDay = endDay;
    }
    
    public String getEndMonth() {
        return endMonth;
    }
    
    public void setEndMonth(String endMonth) {
        this.endMonth = endMonth;
    }
    
    public String getEndYear() {
        return endYear;
    }
    
    public void setEndYear(String endYear) {
        this.endYear = endYear;
    }
    
    public Calendar getStartDate() {
        return getCalendar(Integer.parseInt(startDay), 
            Integer.parseInt(startMonth), Integer.parseInt(startYear), true);
    }
    
    public Calendar getEndDate() {
        return getCalendar(Integer.parseInt(endDay), Integer.parseInt(endMonth),
            Integer.parseInt(endYear), false);
    }
    
    public int getStartIndex() {
        return startIndex;
    }
    
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }
    
    public int getCount() {
        return count;
    }
    
    public void setCount(int count) {
        this.count = count;
    }
    
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        reset();
    }
    
    public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {
        
        ActionErrors errors = new ActionErrors();
        
        DateRanges dateRanges = (DateRanges)
            getServlet().getServletConfig().getServletContext().
                getAttribute("dateRanges");

        accountIdentifierAsLong = new Long(PropertyValidator.validateLong(
            errors, "accountIdentifier", accountIdentifier, true, 0, 
            Long.MAX_VALUE));
        PropertyValidator.validateLong(errors, "startDay", startDay, true, 
            DateRanges.FIRST_DAY, DateRanges.LAST_DAY);
        PropertyValidator.validateLong(errors, "startMonth", startMonth, true, 
            DateRanges.FIRST_MONTH, DateRanges.LAST_MONTH);
        PropertyValidator.validateLong(errors, "startYear", startYear, true, 
            DateRanges.FIRST_YEAR, dateRanges.getLastYear());
        PropertyValidator.validateLong(errors, "endDay", endDay, true, 
            DateRanges.FIRST_DAY, DateRanges.LAST_DAY);
        PropertyValidator.validateLong(errors, "endMonth", endMonth, true, 
            DateRanges.FIRST_MONTH, DateRanges.LAST_MONTH);
        PropertyValidator.validateLong(errors, "endYear", endYear, true, 
            DateRanges.FIRST_YEAR, dateRanges.getLastYear());
        
        return errors;
        
    }

    private void reset() {
    
        /* Get current date. */
        Calendar calendar = Calendar.getInstance();
    
        /* Set default values. */
        accountIdentifier = null;
        accountIdentifierAsLong = null;
        startDay = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
        startMonth = Integer.toString(
            calendar.get(Calendar.MONTH) -  Calendar.JANUARY + 1);
        startYear = Integer.toString(calendar.get(Calendar.YEAR));
        endDay = startDay;
        endMonth = startMonth;
        endYear = startYear;
        startIndex = 1;
        count = 10;
        
    }
    
    /**
     * Get a <code>Calendar</code> for a start date or an end date,
     * with the given day, month and year. Start dates are generated with
     * fields <code>HOUR</code>, <code>MINUTE</code> and <code>SECOND</code>
     * set to 0, and <code>AM_PM</code> to <code>AM</code>. End dates are 
     * generated with such fields set to 11, 59, 59 and <code>PM</code>, 
     * respectively. This way, this method can be used to generate a start date
     * and an end date that allow to get all account operations made on a given
     * account between these two dates, and including both. Remember that dates
     * are stored in the database with the information contained in a 
     * <code>Calendar</code> (discarding milliseconds), and not only storing 
     * day, month and year.
     *
     * @param day a day (1..31)
     * @param month a month (1..12)
     * @param year a year
     * @param startDate <code>true</code> if start date; <code>false</code> 
     *        otherwise
     * @return the <code>Calendar</code>
     */
    private Calendar getCalendar(int day, int month, int year,
        boolean startDate) {
    
        Calendar date = Calendar.getInstance();
        
        date.clear();
        date.set(Calendar.DAY_OF_MONTH, day);
        date.set(Calendar.MONTH, Calendar.JANUARY + month - 1);
        date.set(Calendar.YEAR, year);
        
        if (!startDate) {
            date.set(Calendar.HOUR, 11);
            date.set(Calendar.MINUTE, 59);
            date.set(Calendar.SECOND, 59);
            date.set(Calendar.AM_PM, Calendar.PM);
        }

        return date;
        
    }

}
